﻿using MetroDataApi.Models;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Web.Http;



namespace MetroDataApi.Controllers
{
    public class GetProjectLayersController : ApiController
    {
        // GET api/getprojectlayers/5
        public string Get(int prjId)
        {
            var list = ProjectLayersHelper.GetLayers(prjId);
            return JsonConvert.SerializeObject(list);
        }

        // PUT api/getprojectlayers/5
        public string Put(int id, [FromBody]string value)
        {
            var ob = JsonConvert.DeserializeObject<List<dynamic>>(value);
            ProjectLayersHelper.UpdateLayers(id, generateList(id, ob));
            return "Success";
        }

        private List<PrjGeoLayers> generateList(int prjId,List<dynamic> list)
        {
            var layers = new List<PrjGeoLayers>();
            for (int i = 0; i < list.Count; i++)
            {
                var l = new PrjGeoLayers()
                {
                    LayerName = list[i].name,
                    PrjId = prjId,
                    Sequence = i,
                    Json = list[i].data
                };
                layers.Add(l);
            }
            return layers;
            
        }
    }
}
